#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/6/26 8:57
# ===========================================
#       题目名称： 49. 字母异位词分组
#       题目地址： https://leetcode.cn/problems/group-anagrams/
#       题目描述： https://note.youdao.com/s/LWYgPSzT
# ===========================================


class Solution:

    def groupAnagrams(self, strs):
        """
            @param strs {list} 字符串列表
        """
        str_dict = {}  # k 字典排序相同的  v 源数据
        for string in strs:
            temp_string = string
            temp_string = sorted(temp_string, key=lambda x: [ord(c) for c in x], reverse=False)
            k = "".join(temp_string)
            if k in str_dict.keys():
                v = str_dict[k]
                v.append(string)
            else:
                str_dict[k] = [string, ]
        return list(str_dict.values())


if __name__ == '__main__':
    s = Solution()
    # [["bat"],["nat","tan"],["ate","eat","tea"]]
    print("""strs = ["eat", "tea", "tan", "ate", "nat", "bat"] =>""", s.groupAnagrams(strs=["eat", "tea", "tan", "ate", "nat", "bat"]))
    # [["", ""]]
    print("""strs = ["", ""] =>""", s.groupAnagrams(strs=["", ""]))
    # [[""]]
    print("""strs = [""] =>""", s.groupAnagrams(strs=[""]))
    # [["a"]]
    print("""strs = ["a"] =>""", s.groupAnagrams(strs=["a"]))
